<?php
/**
 * Created by PhpStorm.
 * User: gakuin
 * Date: 7/9/16
 * Time: 3:36 PM
 */
namespace app\models;

use app\common\CommonHelper;
use app\controllers\commodityAttributeObject;
use Yii;
use yii\db\ActiveRecord;
use app\models\Pro;
use yii\helpers\ArrayHelper;
class Commodity extends ActiveRecord
{

    public function getPro(){
        return $this->hasOne(Pro::className(),['pro_id'=>'pro_id']);
    }
    public  function  getCommodity($commodity_id){
            $commodities = Commodity::find()->where([
                    'del_flag'=>1,
                    'commodity_id' => $commodity_id,
            ])->asArray()->one();
        return  $commodities;
    }

    public  function  getCommodityById($commodity_id){
        $commodity = Commodity::find()->where([
            'del_flag'=>1,
            'commodity_id' => $commodity_id,
        ])->asArray()->one();
        return  $commodity;
    }

    public function getCommodityList($limit,$is_hot,$searchConditon){
        $hotCommodities=array();
        $newCommodities=array();
        $commodities=array();
        if($limit == 2){
            $hotCommodities=Commodity::find()->where(['del_flag'=>1,'is_hot'=>1,'is_sale'=>1])->limit(2)->orderBy('create_time DESC')->asArray()->all();
            $newCommodities=Commodity::find()->where(['del_flag'=>1,'is_sale'=>1])->limit(2)->orderBy('create_time DESC')->asArray()->all();
            if(!$hotCommodities){
                $hotCommodities=Commodity::find()->where(['del_flag'=>1,'is_sale'=>1])->orderBy('sales_num DESC')->limit(2)->asArray()->all();
            }
            $commodities['hotCommodities']=$hotCommodities;
            $commodities['newCommodities']=$newCommodities;
            return $commodities;
        }else if($is_hot == 1){
            $commodities=Commodity::find()->where(['del_flag'=>1,'is_hot'=>1,'is_sale'=>1])->asArray()->all();
            return $commodities;
        }else if($is_hot == 0){
            $commodities=Commodity::find()->where(['del_flag'=>1,'is_sale'=>1])->orderBy('create_time DESC')->asArray()->all();
            return $commodities;
        }else if($is_hot == 2){
            $commodities=Commodity::find()->where(['del_flag'=>1,'is_sale'=>1])->asArray()->all();
            return $commodities;
        }else if($searchConditon){
            $query = new \yii\db\Query();
            $commodities = $query->where(['del_flag'=>1,'is_sale'=>1])->andWhere(['like','commodity_name',$searchConditon])->from('hcps_commodity')->all();
            return $commodities;
        }

    }

    public function  getCommodityNum($commodity_id){
        $commodity = Commodity::find()->where([
            'del_flag'=>1,
            'commodity_id' => $commodity_id,
        ])->asArray()->one();

        return $commodity['sales_num'];
    }
    //后台获取商品列表数据
    public function getTableData($limit,$offset,$search,$is_sale){
        $total = (new \yii\db\Query())
            ->from('hcps_commodity')
            ->where(['is_sale' => $is_sale,'del_flag' => 1])
            ->count();
        $query=new \yii\db\Query();
        if (!empty($search)) {
            $rows = $query->where(['is_sale' => $is_sale,'del_flag' => 1])->andWhere(['like', 'commodity_name', $search])->from('hcps_commodity')->orderBy('commodity_id DESC')->limit($limit)->offset($offset)->all();
        }else{
            $rows = $query->where(['is_sale' => $is_sale,'del_flag' => 1])->from('hcps_commodity')->orderBy('commodity_id DESC')->limit($limit)->offset($offset)->all();
        }
        $rs=['total'=>$total,'rows'=>$rows];

        return $rs;

    }

    public function getCommodityIds($first_category){
        $commodity_ids = Commodity::find()
            ->where(['first_category' => $first_category,'del_flag' => 1,'is_sale' =>1])
            ->select('commodity_id')
            ->asArray()->all();
        return $commodity_ids;
    }
    /**
     * Finds commodity by commodity_ids
     *
     * @param array $commodity_ids
     * @return array
     */
    public function getCommodities($commodity_ids){
        $commodities = array();
        for($i = 0; $i < count($commodity_ids); $i++){
            $commodity = Commodity::find()
                ->select(['commodity_name','pic_url','price','sales_num','commodity_id'])
                ->where(['commodity_id' => $commodity_ids[$i]])
                ->asArray()
                ->one();
            array_push($commodities,$commodity);
        }
        return $commodities;
    }
}


